const ps = require("child_process");
const fs = require("fs");
const { execSync } = ps;
const remoteUrl = "http://101.132.115.230:4873/";
// const remoteUrl = 'http://172.19.46.231:4873/'
const npmName = "low_code_base_pub_merch";
console.log("正在检查依赖包版本信息");

const hashPkg =
  fs.readdirSync("./node_modules").filter((item) => item === npmName).length ===
  1;

if (!hashPkg) {
  // 如果不存在包
  console.log("未找到依赖包 执行安装");
  execSync(`npm install ${npmName} --registry ${remoteUrl}`);
} else {
  const pkgJson = require(`./node_modules/${npmName}/package.json`);
  const localVersion = pkgJson.version; // 本地包版本
  const remoteVersion = execSync(
    `npm view ${npmName} version --registry ${remoteUrl}`
  ).toString(); // 远程包版本
  const isEqual = remoteVersion.indexOf(localVersion) === 0;
  if (!isEqual) {
    // 如果远程和本地的版本不同
    console.log(`remote package version ${remoteVersion}`);
    console.log(`local package version ${localVersion}`);
    console.log("包版本不一致 执行更新");
    execSync("npm run update:lowcode-local");
  } else {
    console.log("依赖包版本相同 无需更新");
  }
}
